<!--
 * @Description: 全部商品页面组件(包括全部商品,商品分类,商品搜索)
 * @Author: hai-27
 * @Date: 2020-02-07 16:23:00
 * @LastEditors: hai-27
 * @LastEditTime: 2020-03-08 12:11:13
 -->
<template>
  <div class="goods" id="goods" name="goods">
    <!-- 面包屑 -->
    <div class="breadcrumb">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item to="/">首页</el-breadcrumb-item>
        <el-breadcrumb-item>全部商品</el-breadcrumb-item>
        <el-breadcrumb-item v-if="search">搜索</el-breadcrumb-item>
        <el-breadcrumb-item v-else>分类</el-breadcrumb-item>
        <el-breadcrumb-item v-if="search">{{ search }}</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <!-- 面包屑END -->

    <!-- 分类标签 -->
    <div class="nav">
      <div class="product-nav">
        <div class="title">分类</div>
        <el-tabs v-model="activeName" type="card">
          <el-tab-pane
              v-for="item in cardList"
              :key="item.caid"
              :label="item.name"
              :name="''+item.caid"
          />
        </el-tabs>
      </div>
    </div>
    <!-- 分类标签END -->

    <!-- 活动标签 -->
    <div class="nav">
      <div class="product-nav">
        <div class="title">分类</div>
        <el-tabs v-model="aid" type="card">
          <el-tab-pane
              v-for="item in activityList"
              :key="item.aid"
              :label="item.name"
              :name="''+item.aid"
          />
        </el-tabs>
      </div>
    </div>
    <!-- 活动标签END -->

    <!-- 排序依据 -->
    <div class="nav">
      <div class="product-nav">
        <div class="title">排序</div>
        <el-tabs v-model="sortBy" type="card">
          <el-tab-pane
              v-for="item in sortByList"
              :key="item.id"
              :label="item.name"
              :name="''+item.id"
          />
        </el-tabs>
      </div>
    </div>
    <!-- 排序依据END -->

    <!-- 升降序 -->
    <div class="nav">
      <div class="product-nav">
        <div class="title">升降</div>
        <el-tabs v-model="sortType" type="card">
          <el-tab-pane
              v-for="item in sortTypeList"
              :key="item.id"
              :label="item.name"
              :name="''+item.id"
          />
        </el-tabs>
      </div>
    </div>
    <!-- 升降序END -->

    <!-- 主要内容区 -->
    <div class="main">
      <div class="list">
        <MallList :list="goodsList" v-if="goodsList.length>0"></MallList>
        <div v-else class="none-product">抱歉没有找到相关的商品，请看看其他的商品</div>
      </div>
      <!-- 分页 -->
      <div class="pagination">
        <el-pagination
            background
            layout="prev, pager, next"
            :page-size="pageSize"
            :total="total"
            @current-change="currentChange"
        ></el-pagination>
      </div>
      <!-- 分页END -->
    </div>
    <!-- 主要内容区END -->
  </div>
</template>
<script>
export default {
  data() {
    return {
      /**
       * @活动类型id
       * */
      aid: '',
      /**
       * @活动列表
       * */
      activityList: this.$store.state.List.Activity,
      /**
       * @商品类型id
       * */
      caid: 0,
      /**
       * @商品列表
       * */
      goodsList: this.$store.state.List.GoodsVo,
      /**
       * @分类列表
       * */
      cardList: this.$store.state.List.Card,
      /**
       * @旧数据
       * */
      categoryList: "", //分类列表
      categoryID: [], // 分类id
      product: "", // 商品列表
      productList: "",
      total: 10, // 商品总量
      pageSize: 10, // 每页显示的商品数量
      currentPage: 1, //当前页码
      activeName: "0", // 分类列表当前选中的id
      search: "", // 搜索条件
      /**
       * @排序
       * */
      sortBy: '10',
      sortType: '1',
      /**
       * @排序参数列表
       * */
      sortByList: [
        {
          id: 10,
          name: '时间'
        }, {
          id: 5,
          name: '热门'
        },
        {
          id: 4,
          name: '综合'
        },
        {
          id: 2,
          name: '价格'
        },
      ],
      /**
       * @升降序
       * */
      sortTypeList: [
        {
          id: 1,
          name: '降序'
        }, {
          id: 0,
          name: '升序'
        }
      ]
    };
  },
  /**
   * @计算数据
   * */
  computed: {
    /**
     * @通用函数
     * */
    helper() {
      return this.$helper;
    },
    /**
     * @全局变量
     * */
    state() {
      return this.$store.state;
    }
  },
  created() {
    // 获取分类列表
    //this.getCategory();
    this.getCardList();
    this.getActivityList();
    if (!this.helper.isNull(this.$route.query.caid)) {
      this.activeName = this.$route.query.caid + "";
      this.getGoodsByCard(this.$route.query.caid);
    }
    if (!this.helper.isNull(this.$route.query.search)) {
      this.activeName = "-1";
      this.getGoodsBySearch(this.$route.query.search);
    } else {
      this.getGoodsByCard(this.$route.query.caid);
    }
  },
  activated() {
    //初始化
  },
  watch: {
    /**
     * @活动
     * */
    aid: function (val) {
      if (this.helper.isNull(this.$route.query.search)) {
        this.getGoodsByCard(this.caid);
      } else {
        this.getGoodsBySearch(this.$route.query.search);
      }
    },
    /**
     * @排序
     * */
    sortType: function (val) {
      if (this.helper.isNull(this.$route.query.search)) {
        this.getGoodsByCard(this.caid);
      } else {
        this.getGoodsBySearch(this.$route.query.search);
      }
    },
    sortBy: function (val) {
      if (this.helper.isNull(this.$route.query.search)) {
        this.getGoodsByCard(this.caid);
      } else {
        this.getGoodsBySearch(this.$route.query.search);
      }
    },
    // 监听点击了哪个分类标签，通过修改分类id，响应相应的商品
    activeName: function (val) {
      if (val === "-1") {
        return;
      }
      this.caid = val;
      // 初始化商品总量和当前页码
      this.total = 0;
      this.currentPage = 1;
      // 更新地址栏链接，方便刷新页面可以回到原来的页面
      this.$router.push({
        path: "/goods",
        query: {caid: this.caid}
      });
    },
    // 监听搜索条件，响应相应的商品
    search: function (val) {
      if (val !== "") {
        //console.log("调用了该方法")
        this.getProductBySearch(val);
      }
    },
    // 监听分类id，响应相应的商品
    caid: function (val) {
      console.log(val)
      //this.getData();
      this.getGoodsByCard(val);
      this.search = "";
    },
    // 监听路由变化，更新路由传递了搜索条件
    $route: function (val) {
      if (val.path === "/goods") {
        if (!this.helper.isNull(this.$route.query.caid)) {
          this.activeName = this.$route.query.caid + "";
        }
        if(!this.helper.isNull(this.$route.query.aid)){
          this.aid=this.$route.query.aid;
        }
        if (!this.helper.isNull(this.$route.query.search)) {
          this.activeName = "-1";
          this.currentPage = 1;
          this.getGoodsBySearch(this.$route.query.search);
        }
        //this.getGoodsByCard(this.$route.query.caid);
      }
    }
  },
  methods: {
    /**
     * @获取商品(搜索栏)
     * */
    getGoodsBySearch(val) {
      let _this = this;
      let data = {};
      if (this.helper.isNull(this.aid) || this.aid === 0||this.aid === "0") {
        data = {
          "name": val,
          "merchant.name": val,
          "merchant.describe.content": val,
          "card.name": val,
          "describe.content": val,
          "activity.name": val,
          "page": _this.currentPage,
          "pageSize": _this.pageSize,
          "sortType": _this.sortType,
          "sortBy": _this.sortBy,
          "state": 1,
        };
      } else {
        data = {
          "name": val,
          "merchant.name": val,
          "merchant.describe.content": val,
          "card.name": val,
          "describe.content": val,
          "activity.name": val,
          "activity.aid": this.aid,
          "page": _this.currentPage,
          "pageSize": _this.pageSize,
          "sortType": _this.sortType,
          "sortBy": _this.sortBy,
          "state": 1,
        };
      }
      this.axios.post(this.$store.state.baseURL + 'client/' + this.$store.state.controller.Goods
          + '/' + this.$store.state.mappings.search + '?'
          + 'OperatorId=1',
          data)
          .then(res => {
            _this.goodsList = res.data.goodsVoList;
            _this.total = res.data.count * _this.pageSize;
            //console.log(res.data);
          })
    },
    /**
     * @获取不同类型商品
     * */
    getGoodsByCard(val) {
      let _this = this;
      let data;
      if (val === "0" || val === 0) {
        if (this.helper.isNull(this.aid) || this.aid === 0||this.aid === "0") {
          data = {
            "page": _this.currentPage,
            "pageSize": _this.pageSize,
            "sortType": _this.sortType,
            "sortBy": _this.sortBy,
            "state": 1,
          };
        } else {
          data = {
            "activity.aid": this.aid,
            "page": _this.currentPage,
            "pageSize": _this.pageSize,
            "sortType": _this.sortType,
            "sortBy": _this.sortBy,
            "state": 1,
          };
        }
      } else {
        if (this.helper.isNull(this.aid) || this.aid === 0||this.aid === "0") {
          data = {
            "card.caid": val,
            "page": _this.currentPage,
            "pageSize": _this.pageSize,
            "sortType": _this.sortType,
            "sortBy": _this.sortBy,
            "state": 1,
          };
        } else {
          data = {
            "card.caid": val,
            "activity.aid": this.aid,
            "page": _this.currentPage,
            "pageSize": _this.pageSize,
            "sortType": _this.sortType,
            "sortBy": _this.sortBy,
            "state": 1,
          };
        }
      }
      this.$axios.post(this.$store.state.baseURL + 'client/' + this.state.controller.Goods + '/query?' + 'OperatorId=1',
          data)
          .then(res => {
            _this.goodsList = res.data.goodsVoList;
            _this.total = res.data.count * _this.pageSize;
            //console.log(res.data);
          })
    },
    /**
     * @获取活动组
     * */
    getActivityList() {
      let _this = this;
      this.axios.get(this.$store.state.baseURL + 'client/' + this.$store.state.controller.Activity + '/queryLike?'
          + 'OperatorId=1' + '&state=' + 1 + '&pageSize=' + 100 + '&sortBy=' + 10 + '&sortType=' + 0)
          .then(res => {
            _this.activityList = [];
            _this.activityList.push({
              "aid": 0,
              "name": "全部",
              "creattime": "2022-03-13 12:31:19",
              "state": 1
            });
            for (let i = 0; i < res.data.length; i++) {
              _this.activityList.push(res.data[i]);
            }
            //console.log(res.data);
          })
    },
    /**
     * @获取分类组
     * */
    getCardList() {
      let _this = this;
      this.axios.get(this.$store.state.baseURL + 'client/' + this.$store.state.controller.Card + '/queryLike?'
          + 'OperatorId=1' + '&state=' + 1 + '&pageSize=' + 100 + '&sortBy=' + 10 + '&sortType=' + 0)
          .then(res => {
            _this.cardList = [];
            _this.cardList.push({
              "caid": -1,
              "name": "搜索",
              "creattime": "2022-03-13 12:31:19",
              "state": 1
            });
            _this.cardList.push({
              "caid": 0,
              "name": "全部",
              "creattime": "2022-03-13 12:31:19",
              "state": 1
            });
            for (let i = 0; i < res.data.length; i++) {
              _this.cardList.push(res.data[i]);
            }
            //console.log(res.data);
          })
    },
    // 返回顶部
    backtop() {
      const timer = setInterval(function () {
        const top = document.documentElement.scrollTop || document.body.scrollTop;
        const speed = Math.floor(-top / 5);
        document.documentElement.scrollTop = document.body.scrollTop =
            top + speed;

        if (top === 0) {
          clearInterval(timer);
        }
      }, 20);
    },
    // 页码变化调用currentChange方法
    currentChange(currentPage) {
      this.currentPage = currentPage;
      if (!this.helper.isNull(this.$route.query.search)) {
        this.getGoodsBySearch(this.$route.query.search);
      } else {
        this.getGoodsByCard(this.$route.query.caid)
      }
      this.backtop();
    },
    // 向后端请求分类列表数据
    getCategory() {
      this.$axios
          .post("/api/product/getCategory", {})
          .then(res => {
            const val = {
              category_id: 0,
              category_name: "全部"
            };
            const cate = res.data.category;
            cate.unshift(val);
            this.categoryList = cate;
          })
          .catch(err => {
            return Promise.reject(err);
          });
    },
    // 向后端请求全部商品或分类商品数据
    getData() {
      // 如果分类列表为空则请求全部商品数据，否则请求分类商品数据
      const api =
          this.categoryID.length == 0
              ? "/api/product/getAllProduct"
              : "/api/product/getProductByCategory";
      this.$axios
          .post(api, {
            categoryID: this.categoryID,
            currentPage: this.currentPage,
            pageSize: this.pageSize
          })
          .then(res => {
            this.product = res.data.Product;
            this.total = res.data.total;
          })
          .catch(err => {
            return Promise.reject(err);
          });
    },
    // 通过搜索条件向后端请求商品数据
    getProductBySearch() {
      this.$axios
          .post("/api/product/getProductBySearch", {
            search: this.search,
            currentPage: this.currentPage,
            pageSize: this.pageSize
          })
          .then(res => {
            this.product = res.data.Product;
            this.total = res.data.total;
          })
          .catch(err => {
            return Promise.reject(err);
          });
    }
  }
};
</script>

<style scoped>
.goods {
  background-color: #f5f5f5;
}

/* 面包屑CSS */
.el-tabs--card .el-tabs__header {
  border-bottom: none;
}

.goods .breadcrumb {
  height: 50px;
  background-color: white;
}

.goods .breadcrumb .el-breadcrumb {
  width: 1225px;
  line-height: 30px;
  font-size: 16px;
  margin: 0 auto;
}

/* 面包屑CSS END */

/* 分类标签CSS */
.goods .nav {
  background-color: white;
}

.goods .nav .product-nav {
  width: 1225px;
  height: 40px;
  line-height: 40px;
  margin: 0 auto;
}

.nav .product-nav .title {
  width: 50px;
  font-size: 16px;
  font-weight: 700;
  float: left;
}

/* 分类标签CSS END */

/* 主要内容区CSS */
.goods .main {
  margin: 0 auto;
  max-width: 1225px;
}

.goods .main .list {
  min-height: 650px;
  padding-top: 14.5px;
  margin-left: -13.7px;
  overflow: auto;
}

.goods .main .pagination {
  height: 50px;
  text-align: center;
}

.goods .main .none-product {
  color: #333;
  margin-left: 13.7px;
}

/* 主要内容区CSS END */
</style>